#include <set>
using namespace std;
class Solution
{
public:
    bool isHappy(int n)
    {
        set<int> s;
        s.insert(n);
        int m = n;
        while (n != 1)
        {
            int c ;
            m = 0;
            while (n > 0)
            {
                c = n % 10;
                m+= c * c;
                n /= 10;
            }
            n = m;
            if(s.count(n)){
                return false;
            }
            s.insert(m);
        }
        return true;
    }
};